package com.huarui.inventory.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;
import org.ewaf.framework.util.HttpUtil;
import com.huarui.department.model.Department;
import com.huarui.dictionary.DictionaryEnum;
import com.huarui.initial.util.InitialUtil;
import com.huarui.initialItem.util.InitialItemVO;
import com.huarui.inventory.model.Inventory;
import com.huarui.inventory.model.InventoryListModel;
import com.huarui.inventory.service.InventoryService;
import com.huarui.part.model.PartCategory;
import com.huarui.part.model.PartProperty;
import com.huarui.part.service.PartService;
import com.huarui.store.model.Store;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.huarui.util.JsonUtil;

/**
 * xzz
 * 
 * @author Administrator
 * 
 */
public class InventoryAction extends BaseAction {

	private static final long serialVersionUID = 4571014222462844094L;

	private InventoryService inventoryService;
	private SessionMessage sessionMessage;
	private String dir;
	private String sort;
	private int start;
	private int limit;
	private InventoryListModel inventorylistModel;
	private List<Store> storeList;
	private List<Department> departmentList;
	private List<PartCategory> categoryLevelList;
	private Integer storeId;
	private Integer categoryOneLevelId;
	private Integer categoryTwoLevelId;
	private String code;
	Map<String, Object> resultMap = new HashMap<String, Object>();
	private Integer partId;
	private String json;
	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	private Map<String, Object> jsonObject;

	public Map<String, Object> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(Map<String, Object> jsonObject) {
		this.jsonObject = jsonObject;
	}
	public Integer getStoreId() {
		return storeId;
	}

	public void setStoreId(Integer storeId) {
		this.storeId = storeId;
	}

	public Integer getCategoryOneLevelId() {
		return categoryOneLevelId;
	}

	public void setCategoryOneLevelId(Integer categoryOneLevelId) {
		this.categoryOneLevelId = categoryOneLevelId;
	}

	public Integer getCategoryTwoLevelId() {
		return categoryTwoLevelId;
	}

	public void setCategoryTwoLevelId(Integer categoryTwoLevelId) {
		this.categoryTwoLevelId = categoryTwoLevelId;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public List<PartCategory> getCategoryLevelList() {
		return categoryLevelList;
	}

	public void setCategoryLevelList(List<PartCategory> categoryLevelList) {
		this.categoryLevelList = categoryLevelList;
	}

	public List<Department> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}

	public List<Store> getStoreList() {
		return storeList;
	}

	public void setStoreList(List<Store> storeList) {
		this.storeList = storeList;
	}

	public SessionMessage getSessionMessage() {
		return sessionMessage;
	}

	public void setSessionMessage(SessionMessage sessionMessage) {
		this.sessionMessage = sessionMessage;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public InventoryListModel getInventorylistModel() {
		return inventorylistModel;
	}

	public void setInventorylistModel(InventoryListModel inventorylistModel) {
		this.inventorylistModel = inventorylistModel;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	/**
	 * 库存查询 xu 返回指定条数不分页的库存信息
	 * 
	 * @return
	 */
	public String stockFind() {
		jsonObject = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer companyId = sessionMessage.getCompanyId();
		map.put("companyId", companyId);
		map.put("varGroup", DictionaryEnum.GROUP_SOURCE.getGroup());
		List<Map<String, Object>> list = inventoryService.queryForListBySize(
				map, limit);
		jsonObject.put("success", true);
		jsonObject.put("data", list);
		return JSON;
	}

	public String browseInventory() throws IOException {

		HttpServletRequest request = HttpUtil.getRequest();

		sessionMessage = UserHolder.getCurrentSessionMessage();

		Integer companyId = sessionMessage.getCompanyId();

		InventoryListModel inventoryModel = this.getModelUtil(companyId,
				request);

		try {

			inventorylistModel = inventoryService
					.getInventoryByCondition(inventoryModel);


		} catch (Exception e) {
e.printStackTrace();
			resultMap.put("failure", "服务器正则升级中...");
			
			return "json";

		}

		JSONArray root = JSONArray.fromObject(inventorylistModel.getResult());
		// 分页信息
		resultMap.put("totalProperty", inventorylistModel.getTotal());
		resultMap.put("root", root);

		return "json";
	}
	
	private InventoryListModel getModelUtil(Integer companyId,
			HttpServletRequest request) {

		InventoryListModel inventoryListModel = new InventoryListModel();
		inventoryListModel.setPageSize(limit);
		inventoryListModel.setStart(start);
		inventoryListModel.setOrderBy(sort);
		inventoryListModel.setDesc("desc".equals(dir) ? true : false);

		String jsonAll = request.getParameter("jsonAll");

		if (StringUtils.isNotBlank(jsonAll)) {

			JSONObject jsonObject = JSONObject.fromObject(jsonAll);

			String storeId = (String) jsonObject.get("storeId");

			if (StringUtils.isNotBlank(storeId)) {

				inventoryListModel.setStoreId(Integer.valueOf(storeId));

			}

			String level1Name = (String) jsonObject.get("level1Name");

			if (StringUtils.isNotBlank(level1Name)&&!"请选择一级分类".equalsIgnoreCase(level1Name)) {

				inventoryListModel.setLevel1Name(level1Name);

			}

			String level2Name = (String) jsonObject.get("level2Name");

			if (StringUtils.isNotBlank(level2Name)&&!"请选择二级分类".equalsIgnoreCase(level2Name)) {

				inventoryListModel.setLevel2Name(level2Name);

			}

			String code = (String) jsonObject.get("code");

			if (StringUtils.isNotBlank(code)) {

				inventoryListModel.setCode(code);

			}
			String lowerQuantity = (String) jsonObject.get("lowerQuantity");

			if (StringUtils.isNotBlank(lowerQuantity)) {

				inventoryListModel.setMinQuantity(Integer
						.valueOf(lowerQuantity));

			}
			String upperQuantity = (String) jsonObject.get("upperQuantity");

			if (StringUtils.isNotBlank(upperQuantity)) {

				inventoryListModel.setMaxQuantity(Integer
						.valueOf(upperQuantity));

			}

		}

		inventoryListModel.setCompanyId(companyId);

		return inventoryListModel;

	}
	public String extList(){
		ListModel<Inventory> lm = new ListModel<Inventory>();
	    SessionMessage sm=UserHolder.getCurrentSessionMessage();
	    lm.setPageSize(limit==0?10:limit);
	    lm.setPage(start==0?0:start/limit+1);
		lm.setOrderBy(sort==null?"id":sort);
		if (dir != null){
			lm.setDesc(dir.equals("DESC") ? true : false);
		}else{
			lm.setDesc(true);
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("companyId", sm.getCompanyId());
		if(partId!=null)map.put("partId", partId);
		if(json!=null){
			Inventory inventory=JsonUtil.getBean(json, Inventory.class);
			map.put("storeId", inventory.getStoreId());
			map.put("shelf_wildcard", inventory.getShelf());
			map.put("code_prefix", inventory.getCode());
			map.put("source", inventory.getSource());
			map.put("name_wildcard", inventory.getName());
			map.put("brand_wildcard", inventory.getBrand());
			map.put("carModel_wildcard", inventory.getCarModel());
			map.put("origin_wildcard", inventory.getOrigin());
		}
		resultMap=inventoryService.getPagingByConditions(map, lm);
		return "json";
	}

	public void setPartId(Integer partId) {
		this.partId = partId;
	}

	public void setJson(String json) {
		this.json = json;
	}

	
}
